#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

srcdir=${pkg}-src
cd ${srcdir} || exit 1

function array2str { local IFS=","; echo "$*"; }

function gen_clang_wrappers {
    local clang_triple=$1
    local sysroot=$2
    
    for exe in clang clang++; do
        local wrapper_name=${clang_triple}-${exe}
        cat <<EOF > /usr/bin/${wrapper_name}
#!/usr/bin/env bash
if [ "\$1" != "-cc1" ]; then
    ${exe} --target=${clang_triple} --sysroot=${sysroot} "\$@"
else
    ${exe} "\$@"
fi
EOF
        chmod +x /usr/bin/${wrapper_name}
    done
}

function append_target_config {
    local clang_triple=$1
    cat <<EOF >> config.toml

[target.${clang_triple}]
cc = "${clang_triple}-clang"
cxx = "${clang_triple}-clang++"
ar = "${AR}"
ranlib = "${RANLIB}"
linker = "${clang_triple}-clang++"
llvm-config = "${llvm_config_path}"
EOF
}

llvm_config_path=/usr/bin/llvm-config
if [ "${arch}" != "${host_arch}" ]; then
    llvm_config_path=/host/usr/bin/llvm-config
fi

targets=(\"${triple}\")
if [ -d /cross/aarch64 ]; then
    if [ -n "${feature_musl}" ]; then
        targets+=(\"aarch64-unknown-linux-musl\")
        gen_clang_wrappers aarch64-unknown-linux-musl /cross/aarch64/usr
    fi
    if [ -n "${feature_ohos}" ]; then
        targets+=(\"aarch64-unknown-linux-ohos\")
        gen_clang_wrappers aarch64-unknown-linux-ohos /cross/aarch64
    fi
fi

cat <<EOF > config.toml
# see config.toml.example for more possible options

change-id = 136941

[llvm]
link-shared = false # NOTE: if link-shared is true, llvm needs to be built with libLLVM-19.so
download-ci-llvm = false
use-libcxx = true # NOTE: llvm is built with libc++ by default

[build]
extended = true
locked-deps = true
build = "${triple}"
host = ["${triple}"]
target = [$(array2str "${targets[@]}")]

[install]
prefix = "${install_root}/usr"

[rust]
download-rustc = false

[target.${triple}]
cc = "${CC}"
cxx = "${CXX}"
ar = "${AR}"
ranlib = "${RANLIB}"
linker = "${CXX}" # NOTE: ld.lld has no correct ld path
llvm-config = "${llvm_config_path}"
EOF

if [ -n "${feature_musl}" ]; then
    append_target_config aarch64-unknown-linux-musl
    cat <<EOF >> config.toml
musl-root = "/cross/aarch64/usr"
EOF
fi

if [ -n "${feature_ohos}" ]; then
    append_target_config aarch64-unknown-linux-ohos
fi

if [ "${arch}" != "${host_arch}" ]; then
    cat <<EOF >> config.toml

[target.${host_triple}]
cc = "${CC_FOR_BUILD}"
cxx = "${CXX_FOR_BUILD}"
ar = "${AR}"
ranlib = "${RANLIB}"
linker = "${CXX}"
llvm-config = "${llvm_config_path}"
EOF
fi

./x.py build
